package com.example.famhealing.schedule;

import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.example.famhealing.R;
import com.example.famhealing.R.id;
import com.example.famhealing.R.layout;
import com.example.famhealing.R.menu;
import com.example.famhealing.timeline.TimelineActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

//일정 메인 액티비티
public class CalendarActivity extends Activity {
	FrameLayout framelayout;
	LinearLayout screen;
	LinearLayout menubar;
	int flg = 0;
	
	ImageButton backButton;
	ImageButton addButton;
	
	LinearLayout conditionView;
	
	//월별 캘린더 뷰 객체
	CalendarMonthView monthView;
	//월별 캘린더 어댑터
	CalendarMonthAdapter monthViewAdapter;
		
	TextView monthText;
	
	ImageButton plusButton;
	ImageButton monthPrevious;
	ImageButton monthNext;
	
	int curYear;
	int curMonth;
	int curDay;
	
	//달력 밑에 진료일정 표시하기 위함
	private ArrayList<Diagnose_List_Data> DArray_Data;
    private Diagnose_List_Data dData;
    private DiagnoseListAdapter dAdapter;
    
    //달력 밑에 투약일정 표시하기 위함
    private ArrayList<Meditation_List_Data> MArray_Data;
    private Meditation_List_Data mData;
    private MeditationListAdapter mAdapter;
	
	//파싱한 정보 저장할 변수들
	//String error, kind, time, writer, carcino, hospital, type, medicine, weekday, medicinetime, num, memo, dosage, status, condition, weight, temper, pressure;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        
        CalendarMonthAdapter.multiSelect = false;
        
        //우측 메뉴바
        menubar = (LinearLayout)findViewById(R.id.menubar);
		menubar.setVisibility(View.GONE);
		framelayout = (FrameLayout)findViewById(R.id.frameview);
		screen = (LinearLayout)findViewById(R.id.screen);
		screen.setVisibility(View.GONE);
		
        //달력 밑의 상태 요약 화면 터치시 상태 상세 화면으로 넘어감
        conditionView = (LinearLayout)findViewById(R.id.conditionView);
        conditionView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CalendarActivity.this, TimelineActivity.class);
				//선택된 날짜 전달
				intent.putExtra("selectedYear", curYear);
				intent.putExtra("selectedMonth", curMonth+1);
				intent.putExtra("selectedDay", curDay);
				startActivity(intent);
			}
        	
        });
        
        backButton = (ImageButton) findViewById(R.id.backbtn);
        backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        //추가버튼 터치시
        addButton = (ImageButton) findViewById(R.id.addbtn);
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CalendarActivity.this, ScheduleActivity.class);
				//선택된 날짜 전달
				intent.putExtra("selectedYear", curYear);
				intent.putExtra("selectedMonth", curMonth+1);
				intent.putExtra("selectedDay", curDay);
				startActivity(intent);
				
			}
			
		});

        // 월별 캘린더 뷰 객체 참조
        monthView = (CalendarMonthView) findViewById(R.id.monthView);
        monthViewAdapter = new CalendarMonthAdapter(this);
        monthView.setAdapter(monthViewAdapter);

        // 리스너 설정
        monthView.setOnDataSelectionListener(new OnDataSelectionListener() {
			public void onDataSelected(AdapterView parent, View v, int position, long id) {
				// 현재 선택한 일자 정보 표시
				MonthItem curItem = (MonthItem) monthViewAdapter.getItem(position);
				curDay = curItem.getDay();
				
				Toast.makeText(CalendarActivity.this, Integer.toString(curDay), Toast.LENGTH_SHORT).show();

			}
		});

        monthText = (TextView) findViewById(R.id.monthText);
        setMonthText();

        // 이전 월로 넘어가는 이벤트 처리
        monthPrevious = (ImageButton) findViewById(R.id.monthPrevious);
        monthPrevious.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		monthViewAdapter.setPreviousMonth();
        		monthViewAdapter.notifyDataSetChanged();

        		setMonthText();
        	}
        });

        // 다음 월로 넘어가는 이벤트 처리
        monthNext = (ImageButton) findViewById(R.id.monthNext);
        monthNext.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		monthViewAdapter.setNextMonth();
        		monthViewAdapter.notifyDataSetChanged();

        		setMonthText();
        	}
        });
        
        //임의로 삽입한 진료일정 데이터
        DArray_Data = new ArrayList<Diagnose_List_Data>();
        dData = new Diagnose_List_Data("1/20", "09:00", "항암", "세브란스 병원");
        DArray_Data.add(dData);
       
        ListView diagnose_list = (ListView)findViewById(R.id.diagnose_List);
        dAdapter = new DiagnoseListAdapter(this, android.R.layout.simple_list_item_1, DArray_Data);
        diagnose_list.setAdapter(dAdapter);
        
        //임의로 삽입한 투약일정 데이터
        MArray_Data = new ArrayList<Meditation_List_Data>();
        mData = new Meditation_List_Data("약", "월/화/수", "12:00");
        MArray_Data.add(mData);
       
        ListView meditation_list = (ListView) findViewById(R.id.meditation_List);
        mAdapter = new MeditationListAdapter(this, android.R.layout.simple_list_item_1, MArray_Data);
        meditation_list.setAdapter(mAdapter);

    }

    //달력상단에 년,월을 표시해주는 메소드
    private void setMonthText() {
    	
    	curYear = monthViewAdapter.getCurYear();
        curMonth = monthViewAdapter.getCurMonth();

        monthText.setText(curYear + "년" + (curMonth+1) + "월");
    }
    
    
    /* 정보 파싱
    private class GetSchedule extends AsyncTask<Void, Void, String> {	

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String url = "";
			
			
			try {
				
				//Post				
				
				URL urladdr = new URL(url);
				
				//Parsing
				boolean inTitle1 = false, inTitle2 = false, inTitle3 = false, inTitle4 = false, 
						inTitle5 = false, inTitle6 = false, inTitle7 = false, inTitle8 = false, 
						inTitle9 = false, inTitle10 = false, inTitle11 = false, inTitle12 = false, 
						inTitle13 = false, inTitle14 = false, inTitle15 = false, inTitle16 = false, 
						inTitle17 = false, inTitle18 = false;
		  			
		  		XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
		  			
		  		XmlPullParser parser = parserCreator.newPullParser(); 
		  		parser.setInput(urladdr.openStream(), "UTF-8");
		  		
		  		int parserEvent = parser.getEventType(); 
		  		
		  		String tag;
		  		
		  		while(parserEvent != XmlPullParser.END_DOCUMENT) {
		  			switch(parserEvent) {
		  			case XmlPullParser.START_DOCUMENT:
		  				break;
		  			case XmlPullParser.END_DOCUMENT:
		  				break;
		  			case XmlPullParser.START_TAG:
		  				tag = parser.getName();
		  				if(tag.equals("error")) {
		  					inTitle1 = true;
		  				} else if(tag.equals("kind")) {
		  					inTitle2 = true;
		  				} else if(tag.equals("time")) {
		  					inTitle3 = true;
		  				} else if(tag.equals("writer")) {
		  					inTitle4 = true;
		  				} else if(tag.equals("carcino")) {
		  					inTitle5 = true;
		  				} else if(tag.equals("hospital")) {
		  					inTitle6 = true;
		  				} else if(tag.equals("type")) {
		  					inTitle7 = true;
		  				} else if(tag.equals("medicine")) {
		  					inTitle8 = true;
		  				} else if(tag.equals("weekday")) {
		  					inTitle9 = true;
		  				} else if(tag.equals("medicinetime")) {
		  					inTitle10 = true;
		  				} else if(tag.equals("num")) {
		  					inTitle11 = true;
		  				} else if(tag.equals("memo")) {
		  					inTitle12 = true;
		  				} else if(tag.equals("dosage")) {
		  					inTitle13 = true;
		  				} else if(tag.equals("status")) {
		  					inTitle14 = true;
		  				} else if(tag.equals("condition")) {
		  					inTitle15 = true;
		  				} else if(tag.equals("weight")) {
		  					inTitle16 = true;
		  				} else if(tag.equals("temper")) {
		  					inTitle17 = true;
		  				} else if(tag.equals("pressure")) {
		  					inTitle18 = true;
		  				}
		  				break;
		  			case XmlPullParser.TEXT:
		  				//tag = parser.getName();
		  				if(inTitle1) {
		  					error = parser.getText();
		  					inTitle1 = false;
		  				} else if(inTitle2) {
		  					kind = parser.getText();
		  					inTitle2 = false;
		  				}
		  				if(kind.equals("1")) {
		  					if(inTitle3) {
		  						time = parser.getText();
		  						inTitle3 = false;
		  					} else if(inTitle4) {
		  						writer = parser.getText();
		  						inTitle4 = false;
		  					} else if(inTitle5) {
		  						carcino = parser.getText();
		  						inTitle5 = false;
		  					} else if(inTitle6) {
		  						hospital = parser.getText();
		  						inTitle6 = false;
		  					} else if(inTitle7) {
		  						type = parser.getText();
		  						inTitle7 = false;
		  					}
		  				} else if(kind.equals("2")) {
		  					if(inTitle3) {
		  						time = parser.getText();
		  						inTitle3 = false;
		  					} else if(inTitle4) {
		  						writer = parser.getText();
		  						inTitle4 = false;
		  					} else if(inTitle8) {
		  						medicine = parser.getText();
		  						inTitle8 = false;
		  					} else if(inTitle9) {
		  						weekday = parser.getText();
		  						inTitle9 = false;
		  					} else if(inTitle10) {
		  						medicinetime = parser.getText();
		  						inTitle10 = false;
		  					} else if(inTitle11) {
		  						num = parser.getText();
		  						inTitle11 = false;
		  					}
		  				} else if(kind.equals("3")) {
		  					if(inTitle3) {
		  						time = parser.getText();
		  						inTitle3 = false;
		  					} else if(inTitle4) {
		  						writer = parser.getText();
		  						inTitle4 = false;
		  					} else if(inTitle12) {
		  						memo = parser.getText();
		  						inTitle12 = false;
		  					} else if(inTitle13) {
		  						dosage = parser.getText();
		  						inTitle13 = false;
		  					} else if(inTitle14) {
		  						status = parser.getText();
		  						inTitle14 = false;
		  					} else if(inTitle15) {
		  						condition = parser.getText();
		  						inTitle15 = false;
		  					} else if(inTitle16) {
		  						weight = parser.getText();
		  						inTitle16 = false;
		  					} else if(inTitle17) {
		  						temper = parser.getText();
		  						inTitle17 = false;
		  					} else if(inTitle18) {
		  						pressure = parser.getText();
		  						inTitle18 = false;
		  					}
		  				}
		  				break;
		  			case XmlPullParser.END_TAG:
		  				tag = parser.getName();
	  					if(tag.equals("schedule")) {
	  						if(kind.equals("1")) {
	  							
	  						} else if(kind.equals("2")) {
	  							
	  						} else if(kind.equals("3")) {
	  							
	  						}
	  					}
		  				break;
		  			}
		  			parserEvent = parser.next();	
		  		}
		  			
		  	} catch (Exception e) {
		  			Log.e("dd", "Error in network call", e);
		  	}
			
			return error;
		}
		
		protected void onPostExecute(String error) {
			
			if(error.equals("0")){
				
				Toast.makeText(CalendarActivity.this, "일정 불러오기 성공!", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(CalendarActivity.this, "일정 불러오기 실패!", Toast.LENGTH_SHORT).show();
			}
			
			super.onPostExecute(error);
		}
	}
	*/
    
    public void onClick (View v) {
		Animation ani = null;
		Intent intent;
		switch(v.getId()) {
		case R.id.menubtn:
			if (flg == 0) {
				menubar.setVisibility(v.VISIBLE);
				ani = AnimationUtils.loadAnimation(this, R.anim.down);
				menubar.startAnimation(ani);
				flg = 1;
				screen.setVisibility(View.VISIBLE);
			} else if (flg == 1){
				ani = AnimationUtils.loadAnimation(this, R.anim.up);
				menubar.startAnimation(ani);
				menubar.setVisibility(v.GONE);
				flg = 0;
				screen.setVisibility(View.GONE);
			}
			break;
		case R.id.screen:
			if (flg == 1) {
				ani = AnimationUtils.loadAnimation(this, R.anim.up);
				menubar.startAnimation(ani);
				menubar.setVisibility(v.GONE);
				flg = 0;
				screen.setVisibility(View.GONE);
			}
			break;
		case R.id.frameview:
			if (flg == 1) {
				ani = AnimationUtils.loadAnimation(this, R.anim.up);
				menubar.startAnimation(ani);
				menubar.setVisibility(v.GONE);
				flg = 0;
				conditionView.setClickable(true);
				monthView.setClickable(true);
				monthPrevious.setClickable(true);
				monthNext.setClickable(true);
			}
			break;
		case R.id.schedulebtn:
			intent = new Intent(this, com.example.famhealing.schedule.CalendarActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
			break;
		}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
